package com.ccjq.shosestore.core.dbmanager;

import java.sql.*;

public class DbConnection {
    private static final String DRIVER;
    private static final String URL;
    private static final String USERNAME;
    private static final String PASSWORD;

    static{
        DbConfigReader dbConfigReader=new DbConfigReader();
        DRIVER=dbConfigReader.getValue("driver");
        URL=dbConfigReader.getValue("url");
        USERNAME=dbConfigReader.getValue("username");
        PASSWORD=dbConfigReader.getValue("password");
    }

    public static Connection getConnection()
    {
        Connection conn=null;

        try {
            Class.forName(DRIVER);
            conn= DriverManager.getConnection(URL,USERNAME,PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.print(DRIVER);
        System.out.print(URL);
        System.out.print(USERNAME);
        System.out.print(PASSWORD);
        return conn;
    }

    public static void close(ResultSet rs, Statement st, Connection conn){
        try{
            if(rs!=null){
                rs.close();
            }
            if(st!=null){
                st.close();
            }
            if(conn!=null){
                conn.close();
            }
        }
        catch(SQLException e){
            e.printStackTrace();
        }
    }
}
